<?php
class AdminDepartmentController  extends Zone_Action{
	public function Fields($default=NULL){
		$data = array(
			title 	=> array(
				type 		=> 'CHAR',
				form_label  => 'Tên phòng',			
				no_empty	=> true
			),
			ord 	=> array(
				type 		=> 'INT',
				form_label => 'Thứ tự',
			),
			parent_id 	=> array(
				type 		=> 'INT',
				form_label => 'Trực thuộc',
			),
			desc	=>array(
				type		=>'TEXT',
				form_label	=>'Mô tả tài liệu'
			),
			created_by_id =>array(
				type			=> 'NONE',
				default_value	=> get_user_id()
			)
		);
		/* Neu chinh sua thi cac thong so duoc mac dinh trc */
		if( is_array( $default ) ){
			foreach( $data as $k => $config ){
				if( $config['type'] != 'NONE' && isset( $default[$k] ) ){
					$data[$k]['default_value'] = $default[$k];
				}
			}
		}	
		return $data;	
	}
	
	public function indexAction(){			
		$posts = $this->Model->fetchAll("SELECT `a`.*,
				`b`.`username` as `created_by_name`,
				`b`.`is_deleted` as `created_is_deleted`
			FROM `departments` as `a` 
			LEFT JOIN `users` as `b` 
				ON `a`.`created_by_id`=`b`.`ID` 
			ORDER BY `ord`");
		
		$this->set('departments',$posts);	
	}
	
	public function addAction(){
		$this->removeLayout();
		$posts = $this->Model->fetchAll("SELECT `a`.*,`b`.`username`,`b`.`ID` as `user_id` 
			FROM `departments` as `a` 
			LEFT JOIN `users` as `b` on `a`.`created_by_id`=`b`.`ID` ORDER BY `ord`");
		$this->set('departments',$posts);
		
		if(isPost()){
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->Fields());
			$data = $form->getData();
			if( !is_array($data) ){
				return $this->setJSON(array(
					message => error($data)
				));
			}	

			$this->Model->insert("departments",$data);
			
			return $this->setJSON(array(
				redirect => "#Admin/Department"
			));
		}
	}
	
	public function editAction(){
		$this->removeLayout();
		$posts = $this->Model->fetchAll("SELECT `a`.*,`b`.`username`,`b`.`ID` as `user_id` 
			FROM `departments` as `a` 
			LEFT JOIN `users` as `b` on `a`.`created_by_id`=`b`.`ID` ORDER BY `ord`");
		$this->set('departments',$posts);
		
		$department_id = getInt('ID',0);		
		$post = $this->Model->fetchRow("SELECT * FROM `departments` WHERE `ID`='$department_id'");
		
		if( !$post ){
			return $this->setJSON(array(
				content => error('Phòng ban đã bị xóa hoặc không tồn tại')
			));		
		}

		$this->set('post',$post);		
		
		if(isPost()){
			loadClass("ZData");
			$form = new ZData();
			$form->addFields( $this->fields($post));
			$data = $form->getData();
				
			if( get('parent_id') == $department_id ){
				return $this->setJSON(array(
					message => error('Phòng ban trực thuộc không hợp lệ')
				));
			}
			
			if( !is_array($data)){
				return $this->setJSON(array(
					message => error($data)
				));
			}		
						
			$this->Model->update("departments",$data,"`ID`='$department_id'");
			return $this->setJSON(array(
				redirect	=> "#Admin/Department/"
			));
		}
	}
	

	public function deleteAction(){
		$department_id = getInt("ID",0);	
		$post = $this->Model->fetchRow("SELECT * FROM `departments` WHERE `ID`='$department_id'");
		if( !$post ){
			return $this->setJSON(array(
				alert => 'Phòng ban đã bị xóa hoặc không tồn tại'
			));		
		}	
		
		$this->Model->update("users", array("department_id" => $post['parent_id'] ),"`department_id`='$department_id'");
		$this->Model->update("departments", array("parent_id" => $post['parent_id'] ),"`parent_id`='$department_id'");
		
		$this->Model->delete("departments"," `ID`='$department_id' ");		
		return $this->setJSON(array(
			redirect	=> "#Admin/Department"
		));
	}

}
?>